<?php
namespace app\designer\controller;

use app\admin\library\AdminController;
use cubox\Cubox;


class Plug extends AdminController{
    public function __construct()
    {
        parent::__construct();
        $this->power('designer');
    }

    private function getApp(){
        $app = $this->post('app',$this->check('应用')->type('stirng')->required());
        if (!$this->app->design($app)){
            throw new \Exception('该应用不支持设计');
        }
        return $app;
    }

    public function onList(){
        $project = $this->app->project($this->getApp());
        
        $list  = [];
        if ($project->plug){
            foreach($project->plug as $name => $info){
                $info['name'] =  $name; 
            
                $list[] = $info;
            }
        }
        
        return $list;
    }

    public function onUpdate(){

        //读取全部插件
        $allPlugName  = [];

        $phpPlugPath = ROOT.DS.APP.DS.$this->getApp().DS.'plug';
        
        if (is_dir($phpPlugPath)){
            foreach (glob($phpPlugPath.DS.'*.php') as $file){
                $allPlugName [] = pathinfo($file,PATHINFO_FILENAME);
            }
        }

        $htmlPlugPath = ROOT.DS.APP.DS.$this->getApp().DS.'template'.DS.'plug';
        if (is_dir($htmlPlugPath)){
            $list = scandir($htmlPlugPath);
            foreach ($list as $file){
                if ($file =='.' || $file == '..' ) continue;
                if (is_dir( $htmlPlugPath.DS.$file )) continue;
                $allPlugName [] = $file;
            }
        }

        $project = $this->app->project($this->getApp());

        $currentPlugList = $project->plug;

        if (!$currentPlugList){
            $currentPlugList = [];
        }

        foreach($allPlugName as $key=>$name){
            if (isset($currentPlugList[$name])){
                unset($allPlugName[$key]);
            }
        }

        if (count($allPlugName)>0){
            $allPlugInfo = $this->getDesignerPlugConfig();
            foreach($allPlugName as $name){
                if (isset($allPlugInfo[$name]))  $currentPlugList[$name] = $allPlugInfo[$name];
            }
        }

        if (count($currentPlugList)>0){
            $project->plug = $currentPlugList;
            $project->save();
        }

        return $currentPlugList;
    }

    public function onAllList(){
        return $this->getDesignerPlugConfig();
    }

    private function getDesignerPlugConfig(){
        $plugs = $this->plug('DesignerPlugConfig');
        
        $list =  [];
        foreach($plugs as $app => $plug){
            try{
                foreach($plug->getData() as $name => $info){
                    if (is_array($info)){
                        $info['app'] = $info['app']??$app;
                        $list[$name] =  $info;
                    }
                }
            }catch(\Exception $e){
                
            }
        }

        return $list;
    }

    public function onAdd(){
        $name =  $this->post('name',$this->check('插件名称')->type('string')->required());
        $app = $this->getApp();

        $list = $this->getDesignerPlugConfig();

        if (isset($list[$name])){

            $info = $list[$name];
            $project = $this->app->project($app);
            
            $plugList =  $project->plug;
            
            if (!$plugList){
                $plugList = [];
            }

            if (isset($plugList[$name])){
                throw new \Exception('该插件已经存在');
            }

            if ($info['type']=='php'){
                $filePath = ROOT.DS.APP.DS.$app.DS.'plug';
                if (!is_dir($filePath)) mkdir($filePath,0777,true);
                $filePath.= DS.$name.'.php';
            }

            if ($info['type']=='html'){
                $filePath = ROOT.DS.APP.DS.$app.DS.'template'.DS.'plug';
                if (!is_dir($filePath)) mkdir($filePath,0777,true);
                $filePath .= DS.$name;
            }
            
            file_put_contents($filePath,'');

            $plugList[$name] = $info;
            $project->plug = $plugList;
            $project->save();

            return true;

        }else{
            throw new \Exception('该插件不存在');
        }

    }

    public function onDel(){
        $name =  $this->post('name',$this->check('插件名称')->type('string')->required());
        $app = $this->getApp();
        $project = $this->app->project($app);
        $plugList =  $project->plug;
            
        if (!isset($plugList[$name])){
            throw new \Exception('该插件不存在');
        }

        $info = $plugList[$name];

        if ($info['type']=='php'){
            $filePath = ROOT.DS.APP.DS.$app.DS.'plug';
            $filePath.= DS.$name.'.php';
        }

        if ($info['type']=='html'){
            $filePath = ROOT.DS.APP.DS.$app.DS.'template'.DS.'plug';
            $filePath .= DS.$name;
        }

        unset($plugList[$name]);

        if (file_exists($filePath)){
            unlink($filePath);
        }

        if (count($plugList)>0){
            $project->plug = $plugList;
            $project->save();
        }else{
            $project->plug = [];
            $project->save();
        }

        return true;
    }
}